//
//  ViewController.m
//  CADisplayLink
//
//  Created by Xwoder on 17/1/31.
//  Copyright © 2017年 Xwoder. All rights reserved.
//

#import "ViewController.h"
#import "UIView+ALQuickFrame.h"

@interface ViewController ()

@property (nonatomic, weak) UIImageView *imageView;

@property (nonatomic, assign) NSUInteger index;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];

    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.size = CGSizeMake(270, 37);
    imageView.center = self.view.center;
    [self.view addSubview:imageView];
    self.imageView = imageView;
    
    CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(refreshImage:)];
    // 定义每次触发由target和selector指定的方法之间间隔多少帧
    displayLink.frameInterval = 5;
    [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}

- (void)refreshImage:(CADisplayLink *)displayLink {
    NSString *imageName = [NSString stringWithFormat:@"%tu.png", self.index];
    UIImage *image = [UIImage imageNamed:imageName];
    self.imageView.image = image;
    
    self.index = self.index >= 15 ? 0 : self.index + 1;
}

@end
